<?php defined('SYSPATH') or die('No direct script access.');
/**
* Date Time Helper
*/
class Helper_MyDateTime {
	
	static function &factory() {
	   $o = new Helper_MyDateTime();
	  return $o;	
	}
	
  // Making Date Format from String
	public function mktime_from_string($strdate)
	{
		$d = date_parse($strdate);
		
		if ($d['error_count'] == 0){
		  $retval = mktime($d['hour'], $d['minute'], $d['second'], $d['month'], $d['day'], $d['year']);
		} else {
		  $retval = mktime(0,0,0,0,0,0);
		}
		
		return $retval;
	}
	
  // Making Date Format from MySQL Data
  // Check PHP Date Reference for $date_format (http://php.net/manual/en/function.date.php)
	public function mktime_from_sql($strdate,$date_format='d-m-Y')
	{
		if(!($strdate == '0000-00-00' || $strdate == '0000-00-00 00:00:00' || is_null($strdate)))
		{
			$date = date_create($strdate);
			
			$show_date = date_format($date,$date_format);
			
		} else {
			$show_date = '-';
		}
		
		return $show_date;
	}
	
  // Add some value to date ($value='+1 month' / '+1 day' / '+1 week' / etc)
	public function date_addition($strdate,$value,$format)
	{
		$show_date =  date($format,strtotime($value,strtotime($strdate)));
		
		return $show_date;
	}
	
  // Saving Date to mySQL Data
	public function save_to_sqldate($strdate,$value='Y-m-d')
	{
		if($strdate == '-' || $strdate == '')
		{
			return NULL;
		} else {
			return date($value,$this->mktime_from_string($strdate));
		}
	}
	
  // Saving Date and Time to mySQL Data (datetime / date_only)
	public function save_to_sql_datetime_now($date_format='datetime')
	{
		switch ($date_format)
		{
		case "datetime":
				  $show_date = date("Y-m-d H:i:s");
				  break;
		
		case "date_only":
				  $show_date = date("Y-m-d");
				  break;
		}
		
		return $show_date;
	}
	
  // Find Specific Value From SQL Date String(year, month, day)
  //See http://php.net/manual/en/function.date.php for $value Reference
	public function find_date_value($datestring,$value='Y')
	{
		return date($value, $this->mktime_from_string($datestring));
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}




